﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using CMS.BLL;

public partial class ProductDetail : System.Web.UI.Page
{
    private int nCurrentLanguage = Support.GetCurrentLanguage();
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.SetLabel();
            this.InitData();
        }
    }

    private void SetLabel()
    {
        string sCode = string.Empty;
        string sPrice = string.Empty;
        string sTrademark = string.Empty;
        string sColor = string.Empty;
        sPrice = MultipleLanguage.PRICE[nCurrentLanguage] + ": ";
        sCode = MultipleLanguage.PROCODE1[nCurrentLanguage];
        sTrademark = MultipleLanguage.TRADEMARK[nCurrentLanguage] + ": ";
        sColor = MultipleLanguage.COLOR[nCurrentLanguage] + ": ";

        lblProducts.Text = MultipleLanguage.PRODUCT[nCurrentLanguage];
        lblTitle.Text = MultipleLanguage.PRODUCT[nCurrentLanguage -1];
        lblMap.Text = MultipleLanguage.PRODUCT[nCurrentLanguage -1];
        lblCodetitle.Text = sCode;
        lblColortitle.Text = sColor;
        lblPricetitle.Text = sPrice;
        lblTrademarktitle.Text = sTrademark;


    }

    private void InitData()
    {
        int nID = ConvertData.ConvertToInt(Request.QueryString[Config.PRO_ID]);
        try
        {
            Products obj = new Products();
            obj.LoadById(nID);
            string sImageDefault = obj.Data.ImageDefault;
            string sImage1 = obj.Data.Image1;
            string sImage2 = obj.Data.Image2;
            string sImage3 = obj.Data.Image3;
            string sImage4 = obj.Data.Image4;


            string sColor = ConvertData.ConvertToString(obj.Data.Colors);
            double dSaleOff = ConvertData.ConvertToDouble(obj.Data.PriceSaleOff);
            int isSaleOff = ConvertData.ConvertToInt(obj.Data.IsSalesOff);

            if (sImageDefault == "")
            {
                imgproducs.ImageUrl = Config.IMAGE_NO_IMAGE;
                imgproducs.ImageUrl = Config.IMAGE_NO_IMAGE;
            }

            else
            {
                lbimg.Text = MultipleLanguage.LBL_lbimg[nCurrentLanguage];
                imgproducs.ImageUrl = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD + obj.Data.ImageDefault;
                urlImg.NavigateUrl = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD + obj.Data.ImageDefault;

                lbimg2.Text = MultipleLanguage.LBL_lbimg[nCurrentLanguage];
                imgproducs2.ImageUrl = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD + obj.Data.Image1;
                urlImg2.NavigateUrl = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD + obj.Data.Image1;

                lbimg3.Text = MultipleLanguage.LBL_lbimg[nCurrentLanguage];
                imgproducs3.ImageUrl = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD + obj.Data.Image2;
                urlImg3.NavigateUrl = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD + obj.Data.Image2;

                lbimg4.Text = MultipleLanguage.LBL_lbimg[nCurrentLanguage];
                imgproducs4.ImageUrl = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD + obj.Data.Image3;
                urlImg4.NavigateUrl = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD + obj.Data.Image3;

                lbimg5.Text = MultipleLanguage.LBL_lbimg[nCurrentLanguage];
                imgproducs5.ImageUrl = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD + obj.Data.Image4;
                urlImg5.NavigateUrl = Config.ROOT + Config.IMAGE_PRODUCT_DEFAULT_UPLOAD + obj.Data.Image4;
            }

            lblProducts.Text = obj.Data.ProductCode;
            lblTrademark.Text = GetTrademark(obj.Data.TradeMarkID).ToUpper();

            string sSpec = "";
            switch (nCurrentLanguage)
            {
                case 1:
                    lblProducts.Text = ConvertData.ConvertToString(obj.Data.ProductName1).ToUpper();
                    sSpec = ConvertData.ConvertToString(obj.Data.Specifications1);

                    break;
                case 2:
                    lblProducts.Text = ConvertData.ConvertToString(obj.Data.ProductName2).ToUpper();
                    sSpec = ConvertData.ConvertToString(obj.Data.Specifications2);

                    break;
                case 3:
                    lblProducts.Text = ConvertData.ConvertToString(obj.Data.ProductName3).ToUpper();
                    sSpec = ConvertData.ConvertToString(obj.Data.Specifications3);

                    break;
            }
            if (sSpec.Length > 0)
            {
                ltrspecial.Text = sSpec;
            }
            else
            {
                ltrspecial.Text = MultipleLanguage.SEARCH_NOITEM[nCurrentLanguage - 1];
            }

            double dPrice = ConvertData.ConvertToDouble(obj.Data.Price);
            if (dPrice > 0)
            { lblPrice.Text = Support.FormatCurrency(dPrice) + " " + "₫"; }
            else
            {
                lblPrice.Text = MultipleLanguage.PRICE_CALL[nCurrentLanguage - 1];
            }
            lnkProductName.Text = ConvertData.ConvertToString(obj.Data.ProductName1);
            lblCode.Text = ConvertData.ConvertToString(obj.Data.ProductCode);



            if (sColor.Length > 0)
            {
                this.ConvertColors(sColor);
            }

        }
        catch { Response.Redirect("~/Default.aspx"); }
    }

    private string GetTrademark(int nID)
    {
        ProductTradeMark obj = new ProductTradeMark();
        obj.LoadById(nID);
        string sReturn = obj.Data.TradeMarkName;
        return sReturn;
    }


    private void ConvertColors(string sColor)
    {
        if (sColor.Length > 0)
        {
            string sColorDetail = "";
            int nCurrentLang = Support.GetCurrentLanguage();

            string[] sColorID = sColor.Split(',');
            int nTotalColor = sColorID.Length;

            for (int y = 0; y < nTotalColor; y++)
            {
                ProductColor objColor = new ProductColor();
                objColor.LoadById(ConvertData.ConvertToInt(sColorID[y]));
                switch (nCurrentLang)
                {
                    case 1:
                        sColorDetail = sColorDetail + objColor.Data.ColorName1 + ",";
                        break;
                    case 2:
                        sColorDetail = sColorDetail + objColor.Data.ColorName2 + ",";
                        break;
                }
            }
            if (sColorDetail.Length > 0)
                sColorDetail = ConvertData.TruncateString(sColorDetail, sColorDetail.Length - 1);
            lblColor.Text = sColorDetail;
        }
    }
}